package org.eclipse.mobileapplicationdeveloper.action;


import org.eclipse.core.resources.IFile;
import org.eclipse.emf.common.command.BasicCommandStack;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.TreePath;
import org.eclipse.jface.viewers.TreeSelection;
import org.eclipse.jface.window.Window;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.mobileapplicationdeveloper.generator.AbstractGenerator;
import org.eclipse.mobileapplicationdeveloper.generator.GeneratorFactory;
import org.eclipse.mobileapplicationdeveloper.wizard.WizardGenerateCode;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IActionDelegate;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;

import MobileApplicationDeveloper.model.AppMobile;
import MobileApplicationDeveloper.model.impl.ContainerImpl;
import MobileApplicationDeveloper.model.impl.ModelFactoryImpl;
import MobileApplicationDeveloper.model.util.ModelAdapterFactory;

public class GenerateApplicationModelAction implements IObjectActionDelegate {

	private Shell shell;
	
	/**
	 * Constructor for Action1.
	 */
	public GenerateApplicationModelAction() {
		super();
	}

	/**
	 * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
	 */
	public void setActivePart(IAction action, IWorkbenchPart targetPart) {
		shell = targetPart.getSite().getShell();
	}

	/**
	 * @see IActionDelegate#run(IAction)
	 */
	public void run(IAction action) {
		WizardDialog dialog = new WizardDialog(shell, new WizardGenerateCode());
		
//		 WizardDialog wizardDialog = new WizardDialog(shell,
//			      new WizardGenerateCode());
			    if (dialog.open() == Window.OK) {
			    	MessageDialog.openInformation(
			    			shell,
			    			"Mobile Application Developer",
			    			"Code Generated Succesfully.");
			      System.out.println("Ok pressed");
			      try {
			    	  
			    	  
			    	  IWorkbenchWindow window = PlatformUI.getWorkbench()
			    			  .getActiveWorkbenchWindow();
			    	  
			    	  ISelection selection = window.getSelectionService().getSelection(
			    			  "org.eclipse.ui.navigator.ProjectExplorer");
			    	  
			    	  TreePath[] paths = ((TreeSelection) selection).getPaths();
			    	  TreePath p = paths[0];
			    	  Object last = p.getLastSegment();
			    	  
			    	  if (last instanceof IFile) {
			    		  String ipath = ((IFile) last).getLocationURI().toString();
			    		  
			    		  ModelFactoryImpl.eINSTANCE.createAppMobile();
			    		  AdapterFactoryEditingDomain domain = 
			    				  new AdapterFactoryEditingDomain(new ModelAdapterFactory(), new BasicCommandStack());
			    		  Resource resource = domain.createResource(ipath);
			    		  resource.load(null);
			    		  
			    		  ContainerImpl container = (ContainerImpl)resource.getContents().get(0);
			    		  
			    		  container.eAllContents();
			    		  AppMobile myApp = container.getMyProfile();
			    		  myApp.getAppName();
			    		  //ViewTransformation.getTemplateAndroid(mainView);
			    		  AbstractGenerator generator = GeneratorFactory.getAndroidGenerator();
			    		  generator.createAppWith(myApp);
			    		  
			    	  }
			      }catch (ClassCastException castException){
			    	  System.out.println("El archivo seleccionado no corresponde a un modelo MAD");
			      }
			      catch (Exception blah) {
			    	  System.out.println(blah);
			      }			
			    } else {
			    	MessageDialog.openInformation(
			    			shell,
			    			"Mobile Application Developer",
			    			"Code Generate Canceled.");
			    	
			       }
		
  	}

	/**
	 * @see IActionDelegate#selectionChanged(IAction, ISelection)
	 */
	public void selectionChanged(IAction action, ISelection selection) {
	}

}
